<?php
/**
 * Created by PhpStorm.
 * User: 
 * Date: 
 * Time: 
 */

namespace app\services\model;


use think\Model;
use think\Db;

class AdminUsers extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'admin_users';
    /**
     * [findByUserid description]
     * @param  [type] $userId  [description]
     * @param  [type] $reqData [description]
     * @return [type]          [description]
     */
    public function findByUserid($userId){
        $info = self::find([
            "id"=>$userId
        ]);

        return $info->getData();
    }  

    /**
     * [findByUseridStr description]
     * @param  [type] $userId  [description]
     * @param  [type] $reqData [description]
     * @return [type]          [description]
     */
    public function findByUseridStr($userId){
        $object = Db::table("admin_users")
            ->whereIn("id",$userId);
        $info = $object
            ->field([
                "id",
                "account",
                "name",
                "tel",
            ])
            ->select();

        return $info;
    }

    /**
     * [userList description]
     * @param  [type] $userId  [description]
     * @param  [type] $reqData [description]
     * @return [type]          [description]
     */
    public function userList($userId,$data){
        $object = Db::table("admin_users user")
            ->join("admin_roles role",'user.role_id=role.id')
            // ->where("user.status",1)
            ;

        if(!empty($data['title'])){
            $title = $data['title'];
            $object = $object->where("user.account like '%$title%' or user.tel like '%$title%' or user.name like '%$title%'");
        }
        if(!empty($data['roleId'])){
            $object = $object->where("user.role_id",$data['roleId']);
        }
        if(isset($data['page'])&&$data['page']){
            $page = $data['page'];
        }
        else{
            $page = 1;
        }
        $info = $object
            ->field([
                "user.id",
                "user.account",
                "user.name",
                "user.tel",
                "user.role_id as roleId",
                "role.title as role",
                "user.status"
            ])
            ->order("user.id desc")
            ->paginate($data['pageSize'], false, ['page '=>$page]);

        return $info;
    } 

    /**
     * [userDetail description]
     * @param  [type] $userId  [description]
     * @param  [type] $reqData [description]
     * @return [type]          [description]
     */
    public function userDetail($userId,$data){
        $object = Db::table("admin_users user")
            ->join("admin_roles role",'user.role_id=role.id')
            ->where("user.id",$data['id']);

        
        $info = $object
            ->field([
                "user.id",
                "user.account",
                "user.name",
                "user.tel",
                "role.title as role",
            ])
            ->find();

        return $info;
    }

    /**
     * [resetPassword description]
     * @param  [type] $userId [description]
     * @param  [type] $data   [description]
     * @return [type]         [description]
     */
    public function resetPassword($userId,$data){
        //禁止修改管理员的
        $object = Db::table("admin_users user")
            ->where("user.id",$data['id']);
        $info = $object->find();

        if(!$info){
            return 2006;
        }
        $ret = $this->isAdmin($data['id']);
        if($ret){
            return $ret;
        }

        $ret = $this->isAdmin($userId);
        if($ret!=2007){
            return 2008;
        }

        $new['password'] = md5($data['initPassword']);
        Db::table("admin_users user")
            ->where("user.id",$data['id'])
            ->update($new);
        return 0;
    }

    /**
     * [isAdmin description]
     * @param  [type]  $userId [description]
     * @return boolean         [description]
     */
    private function isAdmin($userId){
        //查询用户角色id
        $object = Db::table("admin_users user")
            ->where("user.id",$userId);
        $info = $object->find();

        if(!$info){
            return 2006;
        }

        //查询角色
        $roleInfo = Db::table("admin_roles")
            ->where("id",$info['role_id'])
            ->find();
        if(!$roleInfo){
            return 2005;
        }
        //验证角色
        if($roleInfo['title']=='管理员'){
            return 2007;

        }
        return 0;
    }

    /**
     * [addUser description]
     * @param [type] $userId [description]
     * @param [type] $data   [description]
     */
    public function addUser($userId,$data){
       $ret = $this->isAdmin($userId);
        if($ret!=2007){
            return 2008;
        }
        //有权限
        if(!empty($data['id'])){
            //编辑
            try{
                $new['account'] = $data['account'];
                $new['tel'] = $data['tel'];
                $new['name'] = $data['name'];
                $new['role_id'] = $data['roleId'];
                Db::table("admin_users")->where("id",$data['id'])
                ->update($new);
                return 0;
            }
            catch(\Exception $e){
                return 2009;
            }
        }
        else{
            //新增 
            try{
                $new['account'] = $data['account'];
                $new['tel'] = $data['tel'];
                $new['name'] = $data['name'];
                $new['role_id'] = $data['roleId'];
                $new['status'] = 1;
                $new['password'] = md5($data['initPassword']);
                Db::table("admin_users")->insert($new);
                return 0;
            }
            catch(\Exception $e){
                return 2009;
            }
        }
        return 0;
    }


    /**
     * [delUser description]
     * @param [type] $userId [description]
     * @param [type] $data   [description]
     */
    public function delUser($userId,$data){
         // 当前用户是不是管理员
        $ret = $this->isAdmin($userId);
        if($ret!=2007){
            return 2008;
        }
        //要删除的是不是管理员
        $ret = $this->isAdmin($data['id']);
        if($ret){
            return $ret;
        }
        //有权限
        Db::table("admin_users")
            ->where("id",$data['id'])
            ->update(['status'=>0]);
        return 0;
    }

    /**
     * [updatePassword description]
     * @param  [type] $userId [description]
     * @param  [type] $data   [description]
     * @return [type]         [description]
     */
    public function updatePassword($userId,$data){
        $info = Db::table("admin_users")
            ->where("id",$userId)
            ->find();
        if(!$info){
            return 2001;
        }
        if($info['password'] != $data['oldPassword']){
            return 2011;
        }
        if($info['password'] == $data['newPassword']){
            return 2017;
        }
        //修改
        Db::table("admin_users")
        ->where("id",$userId)
        ->update([
            "password" =>$data['newPassword']
        ]);
        return 0;
    }
     
}